package jcommon.chap9_templating.velocity.loop_condition;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import jcommon.chap9_templating.velocity.demoframework.DefautEngineDemo;
import jcommon.chap9_templating.velocity.loop_condition.bean.Airport;

import org.apache.velocity.VelocityContext;

public class LoopAndConditonDemo extends DefautEngineDemo {

	@Override
	public Reader getTemplateReader() {
		try {
			return new FileReader(LoopAndCondition.class.getResource("table.vm").getFile());
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public VelocityContext getVelocityContext() {
		VelocityContext context = new VelocityContext();
		context.put("airports", getAirPorts());
		context.put("countryCode", "US");
		return context;
	}

	private List getAirPorts() {
		List airports = new ArrayList( );
		airports.add( new Airport(103, "ATL", "Hartsfield Atlanta", 76876128, "US" ) );
		airports.add( new Airport(202, "ORD", "Chicago O'Hare", 66501496, "US" ) );
		airports.add( new Airport(355, "LHR", "London Heathrow", 63338649, "UK" ) );    
		airports.add( new Airport(478, "HND", "Tokyo-Haneda", 61079478, "JP" ) );
		airports.add( new Airport(518, "LAX", "Los Angeles", 56198447, "US" ) );
		airports.add( new Airport(618, "DFW", "Dallas/Fort Worth", 52826304, "US" ) );
		return airports;
	}

}
